﻿CHAT_AS_VERSITION = "1.0"

import flash.external.*
_global.gfxExtensions = true;

//----- 对python接口 begin -----
function add_msg_text(text:String)
{
	instance.MsgLabel.movieClip.label.appendText(text);
	down_scroll_max();
}

function clear_text()
{
	instance.MsgLabel.movieClip.label.clearText();
	down_scroll_max();
}

function set_input_focus(b:Boolean)
{
	if (b == null)
		b = true;
	if (b)
	{
		Selection.setFocus(instance.Input);
		Selection.setFocus(instance.Input.textField);
		Selection.setSelection(instance.Input.textField.text.length, instance.Input.textField.text.length);
	}
	else
		Selection.setFocus(null);
}
//----- 对python接口 end -----

//----- 参数设置 begin -----
//显示区域的大小
var msg_height:Number = 200;
var msg_width:Number = 240;
//最小化按钮，显示聊天区域时的位置
var hide_btn_show_pos_x:Number = 746;
var hide_btn_show_pos_y:Number = 305;
//最小化按钮，不显示聊天区域时的位置
var hide_btn_hide_pos_x:Number = 746;
var hide_btn_hide_pos_y:Number = 380;
//----- 参数设置 end -----

//----- 鼠标、键盘消息 begin -----
var KeyListener:Object = {};
KeyListener.onKeyUp = function ()
{
	switch(Key.getCode())
	{
		case Key.UP:
			SendHistoryIndex = Math.min(SendHistoryIndex+1, SendHistory.length - 1);
			break;
		case Key.DOWN:
			SendHistoryIndex = Math.max(SendHistoryIndex-1, -1);
			break;
		default:
			return;
	}
	if (SendHistoryIndex == -1)
		instance.Input.text = "";
	else
		instance.Input.text = SendHistory[SendHistory.length - 1 - SendHistoryIndex];
	
}
Key.addListener(KeyListener)

var mouseHandler:Object = {};
mouseHandler.onMouseDown = function(button:Number, targetPath:String, mouseIdx:Number, x:Number, y:Number, dblClick:Boolean)
{
	if (button != 2)
		return;
	var obj:Object = path_2_target(targetPath);
	while (1)
	{
		if (obj == null)
			break;
		if (obj.onMouseRightPress != null)
		{
			obj.onMouseRightPress(x, y, dblClick);
			break;
		}
		obj = obj._parent;
	}
}

function path_2_target(path)
{
	if (path == null)
		return null;
	var target:Object = _root;
	var path_list:Array = path.split(".");
	for (var i:Number = 0; i < path_list.length; i++)
		target = target[path_list[i]];
	return target;
}
Mouse.addListener(mouseHandler);
//----- 鼠标、键盘消息 end -----

instance.ChannelMenu.dataProvider = ["公共", "小喇叭"];
instance.ChannelMenu.rowCount = 2;
instance.ChannelMenu.tabEnabled = false;
instance.ChannelMenu.disableFocus = true;
instance.ChannelMenu.addEventListener("click", this, "hide_pop");

var SendHistory:Array = [];
var SendHistoryIndex:Number = -1;

function send()
{
	/*add_msg_text(instance.Input.text + "\n");
	instance.HornMsgLabel1.label.setText(instance.Input.text);
	instance.HornMsgLabel1._visible = true;*/

	var text:String = instance.Input.text;
	var flag:Boolean = false;
	for (var i:Number = 0; i < SendHistory.length; i++)
	{
		if (SendHistory[i] != text)
			continue;
		flag = true;
		break;
	}
	if (!flag)
		SendHistory.push(text);
	while (SendHistory.length > 5)
		SendHistory.shift()
	SendHistoryIndex = -1;

	switch (instance.ChannelMenu.selectedItem)
	{
		case "公共":
			ExternalInterface.call("chat.instance.send", text);
			break;
		case "小喇叭":
			ExternalInterface.call("chat.instance.horn_send", text);
			break;
	}
	instance.Input.text = "";
}
instance.SendBtn.disableFocus = true;
instance.SendBtn.addEventListener("click", this, "send");
instance.Input.addEventListener("submit", this, "send");
instance.Input.transColor = 0x0000ff;

function hide_pop()
{
	instance.QuickReMenu._visible = false;
	instance.EmotePanel._visible = false;
}

function hide()
{
	HideBtn._x = hide_btn_hide_pos_x;
	HideBtn._y = hide_btn_hide_pos_y;
	instance._visible = false;
	instance.QuickReMenu._visible = false;
	instance.EmotePanel._visible = false;
}

function show()
{
	HideBtn._x = hide_btn_show_pos_x;
	HideBtn._y = hide_btn_show_pos_y;
	instance._visible = true;
	instance.QuickReMenu._visible = false;
	instance.EmotePanel._visible = false;
}

function on_hide_click()
{
	if (instance._visible)
		hide();

	else
		show();
}
HideBtn.disableFocus = true;
HideBtn.addEventListener("click", this, "on_hide_click");

//----- 快捷回复 begin -----
instance.QuickReMenu._visible = false;
instance.QuickReMenu.dataProvider = ["大家好，很高兴见到各位！", "快点吧，我等到花儿也谢了。", "你的牌打得太好了！", "你是MM，还是GG？", "下次再玩吧，我要走了。", "再见了，我会想念大家的。"];
instance.QuickReMenu.addEventListener("itemClick", this, "on_quick_re_click");
instance.QuickReMenu.rowCount = instance.QuickReMenu.dataProvider.length;
instance.QuickReMenu.tabEnabled = false;
instance.QuickReMenu.disableFocus = true;

function on_quick_re_click(e:Object)
{
	instance.Input.text = e.item;
	instance.QuickReMenu._visible = false;
	send();
	set_input_focus(true);
}

function show_quick_re()
{
	var flag:Boolean = instance.QuickReMenu._visible;
	hide_pop();
	instance.QuickReMenu._visible = !flag;
}
instance.QuickReBtn.disableFocus = true;
instance.QuickReBtn.addEventListener("click", this, "show_quick_re");
//----- 快捷回复 end -----

//----- 表情选择 begin -----
var emote_panel_inited:Boolean = false;
instance.EmotePanel._visible = false;

var emote_list:Array = [];
for (var i:Number = 0; i < 58; i++)
{
	var one_emote:MovieClip = instance.EmotePanel.attachMovie("OneEmote", "emote"+i.toString(), instance.EmotePanel.getNextHighestDepth(), {_x:9+21*(i%14), _y:4+21*int(i/14)});
	emote_list.push(one_emote);
	one_emote.id = i;
	one_emote.disableFocus = true;
	one_emote.addEventListener("click", this, "on_emote_click");
}

function init_emote_panel()
{
	for (var i:Number = 0; i < emote_list.length; i++)
		emote_list[i].icon.source = "::commres/gfx/emote/" + (i+1).toString() + ".swf";
	emote_panel_inited = true;
}

function on_emote_click(e:Object)
{
	instance.Input.text += "#" + (e.target.id+1).toString();
	instance.EmotePanel._visible = false;
	set_input_focus(true);
}

function show_emote()
{
	var flag:Boolean = instance.EmotePanel._visible;
	hide_pop();
	if (!emote_panel_inited)
		init_emote_panel();
	instance.EmotePanel._visible = !flag;
}
instance.EmoteBtn.disableFocus = true;
instance.EmoteBtn.addEventListener("click", this, "show_emote");
//----- 表情选择 end -----

//----- 显示区的初始化 begin -----
function get_label_y_min():Number
{
	return Math.min(0, msg_height - instance.MsgLabel.movieClip.label.textHeight - 5);
}

function down_scroll_max()
{
	instance.MsgLabel.movieClip._y = get_label_y_min();
}
instance.DownScrollMaxBtn.disableFocus = true;
instance.DownScrollMaxBtn.addEventListener("click", this, "down_scroll_max");

function down_scroll()
{
	instance.MsgLabel.movieClip._y = Math.max(instance.MsgLabel.movieClip._y - 10, get_label_y_min());
}
instance.DownScrollBtn.disableFocus = true;
instance.DownScrollBtn.autoRepeat = true;
instance.DownScrollBtn.addEventListener("click", this, "down_scroll");

function up_scroll()
{
	instance.MsgLabel.movieClip._y = Math.min(0, instance.MsgLabel.movieClip._y + 10);
}
instance.UpScrollBtn.disableFocus = true;
instance.UpScrollBtn.autoRepeat = true;
instance.UpScrollBtn.addEventListener("click", this, "up_scroll");

function on_text_click(param:String)
{
	ExternalInterface.call("chat.instance.on_text_click", param);
}

function init_display()
{
	var Mask:MovieClip = instance.MsgLabel.createEmptyMovieClip("Mask", instance.MsgLabel.getNextHighestDepth());
	
	Mask.lineStyle(0, 0);
	Mask.beginFill(0, 10);
	Mask.moveTo( 0, 0 );
	Mask.lineTo( msg_width, 0);
	Mask.lineTo( msg_width, msg_height );
	Mask.lineTo( 0, msg_height );
	Mask.lineTo( 0, 0 );
	Mask.endFill();

	instance.MsgLabel.movieClip.setMask(Mask);

	instance.MsgLabel.movieClip._xscale = 100;
	instance.MsgLabel.movieClip._yscale = 100;
	instance.MsgLabel._xscale = 100;
	instance.MsgLabel._yscale = 100;
	instance.MsgLabel.movieClip.label.rightClickFunc = on_text_click;

	instance.HornMsgLabel0._visible = false;
	instance.HornMsgLabel0._xscale = 100;
	instance.HornMsgLabel0._yscale = 100;
	instance.HornMsgLabel1._visible = false;
	instance.HornMsgLabel1._xscale = 100;
	instance.HornMsgLabel1._yscale = 100;

}
init_display();
//----- 显示区的初始化 end -----
